package com.card.game;

import java.util.List;
import java.util.Vector;

import com.card.common.CardContants;
import com.google.common.collect.Lists;

public class LsUtils {
	
	public static boolean isZimo(Vector<Card> sours2,Card huCard) {
		Vector<Card> clones = (Vector<Card>)sours2.clone();
		clones.remove(huCard);
		return isHuPai(clones, huCard);
	}
	
	public static boolean isHuPai(Vector<Card> sours2,Card huCard) {
		if(huCard == null) {
			return false;
		}
		if(sours2.size() >= 4 && huCard.getIndex()>1 && huCard.getIndex()<9
				&& isCanHu(sours2)) {
			Vector<Card> clones = (Vector<Card>)sours2.clone();
			if(clones.remove(new Card(huCard.getType(), huCard.getIndex()-1))&&
					clones.remove(new Card(huCard.getType(), huCard.getIndex()+1))
					) {
				boolean isHu = HandUtils.isHuPai(clones);
				if(isHu) {
					if(clones.remove(huCard)) {
						return isHuPai(clones, huCard);
					}
					return true;
				}
			}
		}
		return false;
	}
	
	public static boolean isCanHu(Vector<Card> cards) {
		List<Card> tingCard = Lists.newArrayList();
		for (int i = CardType.WAN; i <= CardType.BING; i++) {
			for (int j = CardContants.Min_Index; j < 10; j++) {
				Card c = new Card(i, j);
				if(KdUtils.isHu(cards, c)) {
					tingCard.add(c);
					if(tingCard.size() >1) {
						return false;
					}
				}
			}
		}
		for (int i = CardType.DONG; i <= CardType.BAI; i++) {
			Card c = new Card(i, 0);
			if(KdUtils.isHu(cards, c)) {
				tingCard.add(c);
				if(tingCard.size() >1) {
					return false;
				}
			}
		}
		return tingCard.size() == 1;
	}
	
	public static void main(String[] args) {
//		String source = "1-7;1-9;1-9;1-9;";
//		Vector<Card> cards = CardUtils.createCardsFromStr(source);
//		Card huCard = new Card(1, 8);
//		System.err.println(isHuPai(cards,huCard));
		
		String source = "1-4;1-4;1-7;1-8;1-9;";
		Vector<Card> cards = CardUtils.createCardsFromStr(source);
		Card huCard = new Card(1, 8);
		System.err.println(isZimo(cards,huCard));
	}
	
}
